package dao.impl;

import dao.UseDao;
import pojo.Users;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoimpl implements UseDao {

    @Override
    public Users userlogin(String name, String password) {
        Users users = new Users();
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.创建数据库的链接  jdbc:mysql://hostname:port/databasename;
            String url = "jdbc:mysql://localhost:3306/my?serverTimezone=GMT%2B8";
            String user = "root";
            String pwd = "1234";
            Connection conn = DriverManager.getConnection(url, user, pwd);

            //3.构造命令并执行命令
            String sql = "select * from user where username=? and userpwd=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                users.setUsername(rs.getString("username"));
                users.setPassword(rs.getString("userpwd"));
                break;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }
}
